//
//  CustomButton.m
//  text
//
//  Created by apple on 15/9/11.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "CustomButton.h"

@interface CustomButton()

{
    UILabel *_titleLabel;
}

@end

@implementation CustomButton

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        _titleLabel = [[UILabel alloc] initWithFrame:self.bounds];
        _titleLabel.textColor = [UIColor blackColor];
        
        [self addSubview:_titleLabel];
    }
    return self;

}

- (void)setTitle:(NSString *)title
{
    _title = [title copy];
    
    _titleLabel.text = title;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    _titleLabel.textColor = [UIColor lightGrayColor];
    
    _state = CustomButtonStateHighlighted;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *t = [touches anyObject];
    
    CGPoint location = [t locationInView:self];
    
    //point不在矩形区域中
    
    if (!CGRectContainsPoint(self.bounds, location))
    {
        _titleLabel.textColor = [UIColor blueColor];
        
        _state = CustomButtonStateNormal;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (_state == CustomButtonStateHighlighted)
    {
        if (_touchUpInside)
        {
            _touchUpInside(self);
        }
    }
    _titleLabel.textColor = [UIColor blueColor];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    _titleLabel.textColor = [UIColor blueColor];
}


@end
